package com.corp.tx;

import com.corp.platform.common.converter.json.JSONBinder;
import org.apache.rocketmq.spring.annotation.RocketMQTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionState;
import org.apache.rocketmq.spring.support.RocketMQUtil;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * Title: Description:
 *
 * @author Keanu
 * @version 1.0 2020/6/18 0018
 */
@Service
@RocketMQTransactionListener(rocketMQTemplateBeanName = "MrRocketMQ")
public class TestTx implements RocketMQLocalTransactionListener {

    @Override
    public RocketMQLocalTransactionState executeLocalTransaction(Message msg, Object arg) {
        System.out.println("执行");
        System.out.println(JSONBinder.toJsonStr(arg));
        return RocketMQLocalTransactionState.COMMIT;
    }

    @Override
    public RocketMQLocalTransactionState checkLocalTransaction(Message msg) {
        System.out.println("检查");
        Map<String,Object> mode = RocketMQUtil.getPayload(msg, HashMap.class);
        System.out.println(JSONBinder.toJsonStr(mode));
        return RocketMQLocalTransactionState.COMMIT;
    }
}
